<?php
namespace Todo\Controller;
use Todo\Service\LoginService;
use Todo\Util\ArrayUtil;
use Think\Controller;
class BaseController extends Controller {
    protected $_user = null;

    protected function _initialize(){
        if(!defined("NOW_TIME")) define('NOW_TIME', $_SERVER['REQUEST_TIME']);

        //加载配置
        $map = $this->_loadConfig();
        C($map);

        //加载当前登录用户
        $this->_loadUser();

    }

    /**
     * 加载配置文件
     * @param bool $map=true 是否映射成关联数组
     * @return array|mixed
     */
    protected function _loadConfig( $map = true){
        $list =  D('Todo/Sys/Config')->select();
        return $map ? ArrayUtil::map($list, 'code', 'value') : $list;
    }

    /**
     * 获取当前登录用户
     * @param bool $forceLogin=false 如果未登录是否强制要求进行登录
     */
    protected function _loadUser($forceLogin=false){
        if($this->_user) return;

        $this->_user = LoginService::getInstance()->getLoginUser($forceLogin);
        if($this->_user){
            define('UID', $this->_user['id']);
            define('TNT_ID', $this->_user['tnt_id']);
        }
    }


    /**
     * 操作失败的返回信息方法
     * @param string $msg      提示信息
     * @param int    $error 错误代码
     * @param string $url 要跳转的URL地址
     */
    protected function apiError($msg = '发生错误', $error = 1, $url = null)
    {
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Methods:POST");
        header("Access-Control-Allow-Headers:x-requested-with,content-type,X-Token");
        $out  =  array();
        $out['error']  =  $error;
        $out['message'] = $msg;
        if($url !== null) {
            $out['url'] = $url;
        }
        $this->ajaxReturn($out);
    }

    /**
     * 操作成功的返回数据方法
     * @param array $data 数据
     * @param string $url 要跳转的URL地址
     * @param string $msg ='' 提示信息
     */
    protected function apiSuccess($msg = '操作成功',$url = null, $data = null)
    {
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Methods:POST");
        header("Access-Control-Allow-Headers:x-requested-with,content-type,X-Token");
        $out  =  array();
        $out['error']  =  0;
        $out['message'] = $msg;
        if($url !== null) $out['url'] = $url;
        if($data !== null) $out['data'] = $data;
        $this->ajaxReturn($out);
    }


    /**
     * 操作成功的返回信息方法
     * @param array $data 数据
     * @param string $url 要跳转的URL地址
     * @param string $msg ='' 提示信息
     */
    protected function apiList($data, $msg = '操作功能')
    {
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Methods:POST");
        header("Access-Control-Allow-Headers:x-requested-with,content-type,X-Token");
        $out  =  array();
        $out['rows'] = $data['list'];
        $out['total'] = $data['total'];
        $out['error']  =  0;
        $out['message'] = $msg;
        $this->ajaxReturn($out);
    }

    /**
     * 操作成功的返回信息方法
     * @param array $data 数据
     * @param string $url 要跳转的URL地址
     * @param string $msg ='' 提示信息
     */
    protected function apiData($data, $msg = '操作功能')
    {
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Methods:POST");
        header("Access-Control-Allow-Headers:x-requested-with,content-type,X-Token");
        $out  =  array();
        $out['data'] = $data;
        $out['error']  =  0;
        $out['message'] = $msg;
        $this->ajaxReturn($out);
    }
}